package com.ebt.m.proposal_v2.manager;

import android.os.Handler;
import android.os.Message;

import com.ebt.m.customer.constant.CustomerConstant;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;


/**
 * Created by Rick.Wang on 2017/10/27.
 */

public class RelationManager {

    private HashSet<Integer> existedRelation = new HashSet<>();

    private static RelationManager instance;

    private RelationManager(){}

    public static RelationManager getInstance(){
        if (instance == null) {
            synchronized (RelationManager.class) {
                if (instance == null) {
                    instance = new RelationManager();
                }
            }
        }
        return instance;
    }

    public void addRelation(int relation){
        existedRelation.add(relation);
    }

    public void removeRelation(int relation){
        existedRelation.remove(relation);
    }
    public boolean checkExist(int relation){
        return existedRelation.contains(relation);
    }

    public boolean checkIfMyselfExist(){
        return existedRelation.contains(0);
    }

    public boolean checkIfCoupleExist(){
        return existedRelation.contains(1);
    }

    //有list就一定有defaultValue
    public int getProperRelationByOptionList(List<Integer> list, int defaultValue){
        if (list == null){
            if (defaultValue < 0 || defaultValue > 6){
                boolean myselfExist = checkIfMyselfExist();
                boolean coupleExist = checkIfCoupleExist();
                if (myselfExist){
                    if (coupleExist){
                        return 2;
                    }else{
                        return 1;
                    }
                }
                if (coupleExist){
                    if (myselfExist){
                        return 2;
                    }else{
                        return 1;
                    }
                }
                return 0;
            }else{
                return defaultValue;
            }
        }else{
            if (defaultValue < 0 || defaultValue > 6){
                return 0;
            }
            if (defaultValue == 0){//自己
                if (checkIfMyselfExist()){
                    for (Integer item : list){
                        if (item == 1){//夫妻
                            if(checkIfCoupleExist()){
                                continue;
                            }else{
                                return 1;
                            }
                        }else{
                            return item;
                        }
                    }
                    return 2;
                }else{
                    return defaultValue;
                }
            }else if(defaultValue == 1){
                if (checkIfCoupleExist()){
                    for (Integer item : list){
                        if (item == 1){//夫妻
                            if(checkIfCoupleExist()){
                                continue;
                            }else{
                                return 1;
                            }
                        }else{
                            return item;
                        }
                    }
                    return 2;
                }else{
                    return defaultValue;
                }
            }
            return defaultValue;
        }
    }
}